package com.xywh.dakaqiandao.mvp.ui.activity;

import android.content.Intent;
import android.content.res.Resources;
import android.graphics.Color;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v7.widget.DefaultItemAnimator;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.alibaba.android.arouter.facade.annotation.Route;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.dds.soap.SoapListener;
import com.dds.soap.SoapParams;
import com.dds.soap.SoapUtil;
import com.google.gson.Gson;
import com.google.gson.JsonSyntaxException;
import com.jess.arms.base.BaseActivity;
import com.jess.arms.base.BaseConstants;
import com.jess.arms.di.component.AppComponent;
import com.jess.arms.utils.ArmsUtils;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.constant.SpinnerStyle;
import com.scwang.smartrefresh.layout.header.ClassicsHeader;
import com.scwang.smartrefresh.layout.listener.OnRefreshListener;
import com.xywh.dakaqiandao.R;
import com.xywh.dakaqiandao.di.component.DaggerQianDaoJiLuComponent;
import com.xywh.dakaqiandao.di.module.QianDaoJiLuModule;
import com.xywh.dakaqiandao.mvp.contract.QianDaoJiLuContract;
import com.xywh.dakaqiandao.mvp.presenter.QianDaoJiLuPresenter;
import com.xywh.dakaqiandao.mvp.ui.adapter.QianDaoJiLuAdapter;
import com.xywh.dakaqiandao.mvp.ui.entity.QianDaoJiLu;

import org.ksoap2.SoapFault;
import org.ksoap2.serialization.SoapObject;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import me.jessyan.armscomponent.commonres.utils.ToolbarUtils;
import me.jessyan.armscomponent.commonres.view.RecycleViewDivider;
import me.jessyan.armscomponent.commonsdk.core.RouterHub;

import static com.jess.arms.base.BaseConstants.BASE_URL;
import static com.jess.arms.base.BaseConstants.NAME_SPACE;
import static com.jess.arms.utils.Preconditions.checkNotNull;


@Route(path = RouterHub.ACTIVITY_QIAN_DAO_JI_LU)
public class QianDaoJiLuActivity extends BaseActivity<QianDaoJiLuPresenter> implements QianDaoJiLuContract.View, OnRefreshListener {


    private static boolean mIsNeedDemo = true;
    @BindView(R.id.empty_text)
    TextView empty_text;
    @BindView(R.id.empty_image)
    ImageView empty_image;

    @BindView(R.id.empty)
    View mEmptyLayout;
    @BindView(R.id.recyclerView)
    RecyclerView mRecyclerView;
    @BindView(R.id.refreshLayout)
    RefreshLayout mRefreshLayout;
    @BindView(R.id.img)
    ImageView img;
    @BindView(R.id.tv_qian_dao_shi_jian_shang_wu)
    TextView tvQianDaoShiJianShangWu;
    @BindView(R.id.tv_qian_dao_di_dian_shang_wu)
    TextView tvQianDaoDiDianShangWu;
    @BindView(R.id.tv_qian_dao_shi_jian_xia_wu)
    TextView tvQianDaoShiJianXiaWu;
    @BindView(R.id.tv_qian_dao_di_dian_xia_wu)
    TextView tvQianDaoDiDianXiaWu;

    private QianDaoJiLuAdapter mAdapter;
    private List<QianDaoJiLu> mDataBeanList = new ArrayList<>();
    private String mAttenceYmd;

    @Override
    public void setupActivityComponent(@NonNull AppComponent appComponent) {
        DaggerQianDaoJiLuComponent //如找不到该类,请编译一下项目
                .builder()
                .appComponent(appComponent)
                .qianDaoJiLuModule(new QianDaoJiLuModule(this))
                .build()
                .inject(this);
    }

    @Override
    public int initView(@Nullable Bundle savedInstanceState) {
        return R.layout.activity_qian_dao_ji_lu; //如果你不需要框架帮你设置 setContentView(id) 需要自行设置,请返回 0
    }

    @Override
    public void initData(@Nullable Bundle savedInstanceState) {

        ToolbarUtils.initToolbarTitleBack(this, getString(R.string.activity_qian_dao_ji_lu));

//        initRefreshLayout();
//        initRecyclerView();
//        initEmpty();
        mAttenceYmd = getIntent().getExtras().getString("AttenceYmd");
        getAttenceInfo();
    }

    //考勤系统-获取当天考勤信息
    private void getAttenceInfo() {

        showLoading();

//        String methodName = "getAttenceInfo";
        String methodName = "GetAttencesSingleUserbyOneDate";

        SoapUtil soapUtil = SoapUtil.getInstance(this);
        soapUtil.setDotNet(true);
        SoapParams params = new SoapParams();
        params.put("identityCardNo", mSharedPreferencesHelper.getString(BaseConstants.USERNAME));
        params.put("AttenceYmd", String.valueOf(mAttenceYmd));

        /**
         * 请求
         */
        soapUtil.callEntry(BASE_URL, NAME_SPACE, methodName, params, new SoapListener() {
            @Override
            public void onSuccess(int statusCode, SoapObject object) {
                hideLoading();

                String result = object.getProperty("GetAttencesSingleUserbyOneDateResult").toString();
                if (result != null) {

                    try {
                        QianDaoJiLu qianDaoJiLu = new Gson().fromJson(result, QianDaoJiLu.class);
                        img.setImageDrawable(getResources().getDrawable(R.drawable.icon_qian_dao_cheng_gong));
                        tvQianDaoShiJianShangWu.setText(qianDaoJiLu.getAttenceYmd() + " " + qianDaoJiLu.getAttenceHmsAm());
                        tvQianDaoDiDianShangWu.setText(qianDaoJiLu.getRegionNameAm());

                        tvQianDaoShiJianXiaWu.setText(qianDaoJiLu.getAttenceYmd() + " " + qianDaoJiLu.getAttenceHmsPm());
                        tvQianDaoDiDianXiaWu.setText(qianDaoJiLu.getRegionNamePm());
                    } catch (JsonSyntaxException e) {
                        e.printStackTrace();
                    } catch (Resources.NotFoundException e) {
                        e.printStackTrace();
                    }

                } else {

                }
            }

            @Override
            public void onFailure(int statusCode, String content, Throwable error) {
                hideLoading();
            }

            @Override
            public void onFailure(int statusCode, SoapFault fault) {
                hideLoading();
            }
        }, getAapiUserName(), getApiPwd());


    }

    @Override
    public void showLoading() {

    }

    @Override
    public void hideLoading() {

    }

    @Override
    public void showMessage(@NonNull String message) {
        checkNotNull(message);
        ArmsUtils.makeText(this, message);

    }

    @Override
    public void launchActivity(@NonNull Intent intent) {
        checkNotNull(intent);
        ArmsUtils.startActivity(intent);
    }

    @Override
    public void killMyself() {
        finish();
    }

    private void initRefreshLayout() {
        mRefreshLayout.setRefreshHeader(new
                ClassicsHeader(this).setSpinnerStyle(SpinnerStyle.FixedBehind).setPrimaryColorId(R.color.public_colorPrimary).setAccentColorId(android.R.color.white));
        mRefreshLayout.setOnRefreshListener(this);
        mRefreshLayout.autoRefresh();
        mRefreshLayout.setEnableLoadMore(false);
    }

    private void initRecyclerView() {
        mRecyclerView.setLayoutManager(new LinearLayoutManager(QianDaoJiLuActivity.this));
        mRecyclerView.addItemDecoration(new RecycleViewDivider(this, LinearLayoutManager.VERTICAL, 10, Color.parseColor("#EEEEEE")));

        mRecyclerView.setItemAnimator(new DefaultItemAnimator());
        mRecyclerView.setAdapter(mAdapter = new QianDaoJiLuAdapter(QianDaoJiLuActivity.this, mDataBeanList));

        mAdapter.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
            }
        });
    }

//    private List<QianDaoJiLu.DataBean> loadModels() {
//        List<QianDaoJiLu.DataBean> lists = new ArrayList<>();
//
//        for (int i = 0; i < 5; i++) {
//
//            QianDaoJiLu.DataBean dataBean = new QianDaoJiLu.DataBean();
//            lists.add(dataBean);
//        }
//
//        return lists;
//    }

    private void initEmpty() {
        empty_image.setImageResource(R.drawable.ic_empty);
        empty_text.setTextColor(getResources().getColor(R.color.public_white));
        empty_text.setText("暂无数据");
    }

    @Override
    public void onRefresh(@NonNull RefreshLayout refreshLayout) {

        //getQianDaoJiLuList();
        mRefreshLayout.finishRefresh();
        mEmptyLayout.setVisibility(View.GONE);

    }

    @Override
    public void getQianDaoJiLuDataSuccess(QianDaoJiLu entityList) {

//        if (entityList != null && entityList.getData() != null && entityList.getData().size() > 0) {
//            mDataBeanList.clear();
//            mDataBeanList.addAll(entityList.getData());
//            mAdapter.notifyDataSetChanged();
//            mEmptyLayout.setVisibility(View.GONE);
//        } else {
//            mEmptyLayout.setVisibility(View.VISIBLE);
//        }

    }

    private void getQianDaoJiLuList() {
        HashMap<String, String> map = new HashMap<>();

        mPresenter.getQianDaoJiLuList(map);
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        // TODO: add setContentView(...) invocation
        ButterKnife.bind(this);
    }
}
